package airthmetic.exercise.tree;

public class _236_二叉树的最近公共祖先 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        // terminal
        if(root == p || root == q || root == null){
            return root;
        }

        // drill down
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);

        // process current logic
        if(left == null){
            return right;
        }

        if(right == null){
            return left;
        }

        return root;
    }
}
